{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3bc107af-ba14-43a7-8544-63183f6b1b0a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.875\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from xgboost import XGBClassifier\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "df = pd.read_excel('信用卡交易数据.xlsx')\n",
    "df.head()\n",
    "X = df.drop(columns='欺诈标签') \n",
    "y = df['欺诈标签']\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123)\n",
    "clf = XGBClassifier(n_estimators=100, learning_rate=0.05)\n",
    "clf.fit(X_train, y_train)\n",
    "y_pred = clf.predict(X_test)\n",
    "score = accuracy_score(y_pred, y_test)\n",
    "print(score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "38ae5226-000f-48a3-9895-5e73c62b11f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pred_proba = clf.predict_proba(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3f2a8a11-446b-44f7-a437-3579c9c50a23",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAiMAAAGdCAYAAADAAnMpAAAAOnRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjEwLjEsIGh0dHBzOi8vbWF0cGxvdGxpYi5vcmcvc2/+5QAAAAlwSFlzAAAPYQAAD2EBqD+naQAAIFhJREFUeJzt3QuUVdV9P/AfDxm0CkoJIDiR+rZBIYJQIMbaRTupLtSVtiExFaQ+q7ERrAqiEB8Ra5GQFiJLjI8+DGiWUpdQjCGyXEQiFTT1FQxBBR8gtAoUFYS5/3XO/AcZGHAG586euffzWet4OWfO4zebce6XffY+t02hUCgEAEAibVNdGAAgI4wAAEkJIwBAUsIIAJCUMA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PGUnBc0ZKUPZjVxtas4fl7W8vWVVVxH33NWlpALTg54xAUbz8cjY6OnUVACQijPCpd9+NeOaZ5rlWy++QA6CZCCN8atiwiFdeaf7rGsgMUNaEET719ts1r9nA0uZ6vPpXvxpx2GHNcy0AWiRhhD395CcRxx6bugoAykTRHwcPALAvwggAkJQwAgAkJYwAAEkZwJrS009HPPhgy3nmRvZZMQDQzISRlP7u7yJ+/etoUdq0ifDIfQCakTDSEnoiLroo4sgjo0U4+eSI//+hhwDQHISRluCCCyKGDk1dBQAkIYw0xmOPRUyfHlFd3TTne+utpjkPALRiwkhj3HprxH/9V9Oft0ePpj8nALQSwkhjbN9e83rddRF9+zbNOY8+umYBgDIljOyPP/7jiK99LXUVAFASPPRsX+6+u6bXonfvmuWll1JXBAAlR8/IvtxzT8SqVXW3tW3rtgoANCFhZF9qn4w6deqnU2979apZAIAmIYzs2PHZYeT44yMGDmy2kgCgnJT3mJFsqu4BB0S0b1//smxZ6goBoOSVdxiZP/+zP6TusMMiTjqpuSoCgLLjNk3mX/814swz6//a7/1eREVFc1cEAGVDGMkcckhEly6pqwCAslTet2kAgOSEEQAgKWEEAEhKGAEAkhJGAICkhBEAIClhBABIShgBAJISRgCApIQRACApYQQASEoYAQCSEkYAgKSEEQAgKWEEAEhKGAEAkhJGAICkhBEAIClhBABIShgBAJISRgCApIQRACApYQQASEoYAQCSEkYAgKSEEQAgKWEEAEhKGAEAkhJGAICkhBEAIClhBABofWFkxowZ0bt37+jYsWMMGjQoli5dus/9P/jgg7jiiivi8MMPj4qKijjuuONi/vz5+1szAFBC2jf2gDlz5sTYsWNj5syZeRCZNm1aVFVVxYoVK6Jbt2577L9t27b40z/90/xrP/3pT6NXr17x5ptvxqGHHtpU3wMAUE5hZOrUqXHxxRfH6NGj8/UslMybNy/uvffeGDdu3B77Z9v/93//N5555pk44IAD8m1ZrwoAQKNv02S9HMuWLYthw4bt3Na2bdt8fcmSJfUe89hjj8XgwYPz2zTdu3ePPn36xG233RY7duzY63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gsbdJ2tW7fGpk2b6iwAQGlqVBjZsGFD3svRvXv3Otuz9bVr19Z7zOLFi+PHP/5xzJo1q8HXmTx5cnTu3HnnUllZ2ZgyAYBWpKizaTZv3hznn39+HkS6du3a4OPGjx8fGzdu3LmsWbOmmGUCAAm1b8zOWaBo165drFu3rs72bL1Hjx577P+73/0uH7g6fPjwnduqq6trLty+faxYsSKOPvroPY6rqKjIFwCg9DWqZ6RDhw7Rv3//WLhwYZ1wka0PHjx4j/1POOGEePHFF+OFF17YuZx99tlxxhln5H92+wUAaFTPSCab1jtq1KgYMGBADBw4MKZNmxZbtmzJZ9dkRo4cGb169crHfWTPIenTp0+d4w899ND8dfftAEB5anQYGTFiRKxfvz4mTpyYD1rt169fLFiwYOeg1tWrV+czbAAAGqJNoVAoRAuXTe3NZtVkg1k7derUdCceMiQim5I8d27EOec03XkBgGjo+7cuDAAgKWEEAEhKGAEAkhJGAICkhBEAIClhBABIShgBAJISRgCApIQRACApYQQASEoYAQCSEkYAgKSEEQAgKWEEAEhKGAEAkhJGAICkhBEAIClhBABIShgBAJISRgCApIQRACApYQQASEoYAQCSEkYAgKSEEQAgKWEEAEhKGAEAkhJGAICkhBEAIClhBABIShgBAJISRgCApIQRACApYQQASEoYAQCSEkYAgKSEEQAgKWEEAEhKGAEAkhJGAICkhBEAIClhBABIShgBAJISRgCApIQRACApYQQASEoYAQCSEkYAgKSEEQAgKWEEAEhKGAEAkhJGAICkhBEAIClhBABIShgBAJISRgCA1hdGZsyYEb17946OHTvGoEGDYunSpXvdd9asWXHaaafFYYcdli/Dhg3b5/4AQHlpdBiZM2dOjB07NiZNmhTLly+Pvn37RlVVVbz33nv17r9o0aL41re+FU899VQsWbIkKisr48/+7M/i7bffbor6AYBWrk2hUCg05oCsJ+TUU0+N6dOn5+vV1dV5wLjyyitj3Lhxn3n8jh078h6S7PiRI0c26JqbNm2Kzp07x8aNG6NTp07RZIYMiViyJGLu3Ihzzmm68wIA0dD370b1jGzbti2WLVuW32rZeYK2bfP1rNejIT788MP45JNPokuXLnvdZ+vWrfk3sOsCAJSmRoWRDRs25D0b3bt3r7M9W1+7dm2DznHddddFz5496wSa3U2ePDlPUrVL1vMCAJSmZp1Nc/vtt8fs2bPj0UcfzQe/7s348ePzLp3aZc2aNc1ZJgDQjNo3ZueuXbtGu3btYt26dXW2Z+s9evTY57FTpkzJw8jPf/7zOPnkk/e5b0VFRb4AAKWvUT0jHTp0iP79+8fChQt3bssGsGbrgwcP3utxd9xxR9xyyy2xYMGCGDBgwOerGAAo356RTDatd9SoUXmoGDhwYEybNi22bNkSo0ePzr+ezZDp1atXPu4j8w//8A8xceLEePDBB/Nnk9SOLTn44IPzBQAob40OIyNGjIj169fnASMLFv369ct7PGoHta5evTqfYVPrrrvuymfh/OVf/mWd82TPKfne977XFN8DAFBOzxlJwXNGAKD1KcpzRgAAmpowAgAkJYwAAEkJIwBAUsIIAJCUMAIAJCWMAABJCSMAQFLCCACQlDACACQljAAASQkjAEBSwggAkJQwAgAkJYwAAEkJIwBAUsIIAJCUMAIAJCWMAABJCSMAQFLCCACQlDACACQljAAASQkjAEBSwggAkJQwAgAkJYwAAEkJIwBAUsIIAJCUMAIAJCWMAABJCSMAQFLCCACQlDACACQljAAASQkjAEBSwggAkJQwAgAkJYwAAEkJIwBAUsIIAJCUMAIAJCWMAABJCSMAQFLCCACQlDACACQljAAASQkjAEBSwggAkJQwAgAkJYwAAEkJIwBAUsIIAJCUMAIAJCWMAABJCSMAQOsLIzNmzIjevXtHx44dY9CgQbF06dJ97v/www/HCSeckO9/0kknxfz58/e3XgCg3MPInDlzYuzYsTFp0qRYvnx59O3bN6qqquK9996rd/9nnnkmvvWtb8WFF14Yzz//fJx77rn58tJLLzVF/QBAK9emUCgUGnNA1hNy6qmnxvTp0/P16urqqKysjCuvvDLGjRu3x/4jRoyILVu2xOOPP75z2x/90R9Fv379YubMmQ265qZNm6Jz586xcePG6NSpUzSZIUMiliyJmDs34pxzmu68AEA09P27UT0j27Zti2XLlsWw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jsKr7zySuGGG24oHHDAAYUXX3yx2Wsv5XY+77zzCjNmzCg8//zzhVdffbVwwQUXFDp37lx46623mr32Um/rWq+//nqhV69ehdNOO61wzjnnNFu95dLOW7duLQwYMKBw5plnFhYvXpy396JFiwovvPBCs9deyu387//+74WKior8NWvjJ554onD44YcXxowZ0+y1tybz588vTJgwofDII49ks2cLjz766D73X7VqVeGggw4qjB07Nn8v/Od//uf8vXHBggVFq7Gkw8jAgQMLV1xxxc71HTt2FHr27FmYPHlyvft/4xvfKJx11ll1tg0aNKhw6aWXFr3Wcmrn3W3fvr1wyCGHFB544IEiVlm+bZ2175AhQwr33HNPYdSoUcJIEdr5rrvuKhx11FGFbdu2NWOV5dfO2b5/8id/Umdb9oY5dOjQotdaKqIBYeTaa68tfOlLX6qzbcSIEYWqqqqi1VWyt2m2bdsWy5Yty28B7PoZN9n6kiVL6j0m277r/pmqqqq97s/+tfPuPvzww/jkk0+iS5cuRay0fNv65ptvjm7dusWFF17YTJWWXzs/9thjMXjw4Pw2Tffu3aNPnz5x2223xY4dO5qx8tJv5yFDhuTH1N7KWbVqVX4r7Mwzz2y2usvBkgTvha3ig/L2x4YNG/JfBNkvhl1l67/5zW/qPWbt2rX17p9tp+naeXfXXXddfi9z9x9+Pn9bL168OH784x/HCy+80ExVlmc7Z2+Kv/jFL+Lb3/52/ua4cuXKuPzyy/OQnT3ZkqZp5/POOy8/7itf+Ur+abDbt2+Pyy67LK6//vpmqro8rN3Le2H26b4fffRRPl6nqZVszwitw+23354PrHz00UfzAWw0nexju88///x8wHDXrl1Tl1PSqqur896nu+++O/r37x8jRoyICRMmxMyZM1OXVlKyQZVZj9OPfvSjWL58eTzyyCMxb968uOWWW1KXxudUsj0j2S/fdu3axbp16+psz9Z79OhR7zHZ9sbsz/61c60pU6bkYeTnP/95nHzyyUWutPza+ne/+1288cYb+Sj6Xd80M+3bt48VK1bE0Ucf3QyVl/7PdDaD5oADDsiPq3XiiSfm/8LMbkd06NCh6HWXQzvfeOONecC+6KKL8vVsxuOWLVvikksuycNfdpuHz29v74WdOnUqSq9IpmT/5rL/+bN/oSxcuLDOL+JsPbu3W59s+677Z5588sm97s/+tXPmjjvuyP81s2DBghgwYEAzVVtebZ1NUX/xxRfzWzS1y9lnnx1nnHFG/udsWiRN8zM9dOjQ/NZMbdjLvPbaa3lIEUSarp2z8WW7B47aAOhj1ppOkvfCQolPG8umgd1///359KRLLrkknza2du3a/Ovnn39+Ydy4cXWm9rZv374wZcqUfMrppEmTTO0tQjvffvvt+XS+n/70p4V3331357J58+aE30VptvXuzKYpTjuvXr06nxH2ne98p7BixYrC448/XujWrVvh1ltvTfhdlF47Z7+Ts3b+yU9+kk8//dnPflY4+uij85mQ7F32uzV7lEK2ZG/7U6dOzf/85ptv5l/P2jhr692n9l5zzTX5e2H2KAZTez+nbH70F7/4xfzNL5tG9qtf/Wrn104//fT8l/OuHnroocJxxx2X759NbZo3b16Cqku7nY888sj8f4jdl+wXDU3/M70rYaR47fzMM8/kjwLI3lyzab7f//7382nVNF07f/LJJ4Xvfe97eQDp2LFjobKysnD55ZcX3n///UTVtw5PPfVUvb9za9s2e83aevdj+vXrl/+9ZD/P9913X1FrbJP9p3j9LgAAZTpmBABoHYQRACApYQQASEoYAQCSEkYAgKSEEQAgKWEEAEhKGAEAkhJGAICkhBEAIClhBABIShgBACKl/wcpWnYFaIZQygAAAABJRU5ErkJggg==",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from sklearn.metrics import roc_curve\n",
    "fpr, tpr, thres = roc_curve(y_test, y_pred_proba[:,1])\n",
    "import matplotlib.pyplot as plt\n",
    "plt.plot(fpr, tpr, color = 'r')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "76457ad2-f6c6-4a21-8b0a-6b69b84a2c3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.8690367205777643)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import roc_auc_score\n",
    "auc = roc_auc_score(y_test, y_pred_proba[:,1])\n",
    "auc"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "979b228a-7385-4b6d-acdc-54acd914d633",
   "metadata": {},
   "source": [
    "参数调优|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "97227c88-f9b7-4784-a1ea-63c7caafccd0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'learning_rate': 0.01, 'max_depth': 3, 'n_estimators': 150}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.model_selection import GridSearchCV  \n",
    "parameters = {'max_depth': [1, 3, 5], 'n_estimators': [50, 100, 150], 'learning_rate': [0.01, 0.05, 0.1, 0.2]}  # 指定模型中参数的范围\n",
    "clf = XGBClassifier()  # 构建模型\n",
    "grid_search = GridSearchCV(clf, parameters, scoring='roc_auc', cv=10)\n",
    "\n",
    "grid_search.fit(X_train, y_train)\n",
    "grid_search.best_params_ "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d9d9af68-e821-4f63-af98-e1ddc9d8f1d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.8563218390804598)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf = XGBClassifier(max_depth=3, n_estimators=150, learning_rate=0.01)\n",
    "clf.fit(X_train, y_train)\n",
    "y_pred_proba = clf.predict_proba(X_test)\n",
    "\n",
    "auc = roc_auc_score(y_test, y_pred_proba[:,1])\n",
    "auc"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "161f031d-590e-4638-96b8-90bf77d81f30",
   "metadata": {},
   "source": [
    "数据量太少导致调优得到的结果比原先低"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2bb7ae5b-6d72-4927-9447-c370522036d2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c38727f-83fd-4adf-a5ff-3b4d031b2cff",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e46a0935-15ed-4cfd-95aa-4b93f2a54980",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9570203214455993"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from lightgbm import LGBMRegressor\n",
    "from sklearn.metrics import r2_score\n",
    "\n",
    "df = pd.read_excel('广告收益数据.xlsx')\n",
    "X = df.drop(columns='收益') \n",
    "y = df['收益']\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123)\n",
    "\n",
    "model = LGBMRegressor(verbosity=-1)\n",
    "model.fit(X_train, y_train)\n",
    "y_pred = model.predict(X_test)\n",
    "\n",
    "r2 = r2_score(y_test, model.predict(X_test))\n",
    "r2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21f699b5-8158-4923-8816-748dc2563235",
   "metadata": {},
   "source": [
    "参数调优"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "351b8d89-1511-404b-8cdd-2919eee0934f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'learning_rate': 0.3, 'n_estimators': 50, 'num_leaves': 31}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.model_selection import GridSearchCV\n",
    "parameters = {'num_leaves': [15, 31, 62], 'n_estimators': [20, 30, 50, 70], 'learning_rate': [0.1, 0.2, 0.3, 0.4]} \n",
    "model = LGBMRegressor(verbosity=-1)\n",
    "grid_search = GridSearchCV(model, parameters,scoring='r2',cv=5)\n",
    "grid_search.fit(X_train, y_train)\n",
    "grid_search.best_params_ "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8aaedf42-b312-4150-9ee3-877ed9054ec2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>#sk-container-id-2 {\n",
       "  /* Definition of color scheme common for light and dark mode */\n",
       "  --sklearn-color-text: #000;\n",
       "  --sklearn-color-text-muted: #666;\n",
       "  --sklearn-color-line: gray;\n",
       "  /* Definition of color scheme for unfitted estimators */\n",
       "  --sklearn-color-unfitted-level-0: #fff5e6;\n",
       "  --sklearn-color-unfitted-level-1: #f6e4d2;\n",
       "  --sklearn-color-unfitted-level-2: #ffe0b3;\n",
       "  --sklearn-color-unfitted-level-3: chocolate;\n",
       "  /* Definition of color scheme for fitted estimators */\n",
       "  --sklearn-color-fitted-level-0: #f0f8ff;\n",
       "  --sklearn-color-fitted-level-1: #d4ebff;\n",
       "  --sklearn-color-fitted-level-2: #b3dbfd;\n",
       "  --sklearn-color-fitted-level-3: cornflowerblue;\n",
       "\n",
       "  /* Specific color for light theme */\n",
       "  --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
       "  --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, white)));\n",
       "  --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
       "  --sklearn-color-icon: #696969;\n",
       "\n",
       "  @media (prefers-color-scheme: dark) {\n",
       "    /* Redefinition of color scheme for dark theme */\n",
       "    --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
       "    --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, #111)));\n",
       "    --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
       "    --sklearn-color-icon: #878787;\n",
       "  }\n",
       "}\n",
       "\n",
       "#sk-container-id-2 {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 pre {\n",
       "  padding: 0;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 input.sk-hidden--visually {\n",
       "  border: 0;\n",
       "  clip: rect(1px 1px 1px 1px);\n",
       "  clip: rect(1px, 1px, 1px, 1px);\n",
       "  height: 1px;\n",
       "  margin: -1px;\n",
       "  overflow: hidden;\n",
       "  padding: 0;\n",
       "  position: absolute;\n",
       "  width: 1px;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-dashed-wrapped {\n",
       "  border: 1px dashed var(--sklearn-color-line);\n",
       "  margin: 0 0.4em 0.5em 0.4em;\n",
       "  box-sizing: border-box;\n",
       "  padding-bottom: 0.4em;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-container {\n",
       "  /* jupyter's `normalize.less` sets `[hidden] { display: none; }`\n",
       "     but bootstrap.min.css set `[hidden] { display: none !important; }`\n",
       "     so we also need the `!important` here to be able to override the\n",
       "     default hidden behavior on the sphinx rendered scikit-learn.org.\n",
       "     See: https://github.com/scikit-learn/scikit-learn/issues/21755 */\n",
       "  display: inline-block !important;\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-text-repr-fallback {\n",
       "  display: none;\n",
       "}\n",
       "\n",
       "div.sk-parallel-item,\n",
       "div.sk-serial,\n",
       "div.sk-item {\n",
       "  /* draw centered vertical line to link estimators */\n",
       "  background-image: linear-gradient(var(--sklearn-color-text-on-default-background), var(--sklearn-color-text-on-default-background));\n",
       "  background-size: 2px 100%;\n",
       "  background-repeat: no-repeat;\n",
       "  background-position: center center;\n",
       "}\n",
       "\n",
       "/* Parallel-specific style estimator block */\n",
       "\n",
       "#sk-container-id-2 div.sk-parallel-item::after {\n",
       "  content: \"\";\n",
       "  width: 100%;\n",
       "  border-bottom: 2px solid var(--sklearn-color-text-on-default-background);\n",
       "  flex-grow: 1;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-parallel {\n",
       "  display: flex;\n",
       "  align-items: stretch;\n",
       "  justify-content: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-parallel-item {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-parallel-item:first-child::after {\n",
       "  align-self: flex-end;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-parallel-item:last-child::after {\n",
       "  align-self: flex-start;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-parallel-item:only-child::after {\n",
       "  width: 0;\n",
       "}\n",
       "\n",
       "/* Serial-specific style estimator block */\n",
       "\n",
       "#sk-container-id-2 div.sk-serial {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "  align-items: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  padding-right: 1em;\n",
       "  padding-left: 1em;\n",
       "}\n",
       "\n",
       "\n",
       "/* Toggleable style: style used for estimator/Pipeline/ColumnTransformer box that is\n",
       "clickable and can be expanded/collapsed.\n",
       "- Pipeline and ColumnTransformer use this feature and define the default style\n",
       "- Estimators will overwrite some part of the style using the `sk-estimator` class\n",
       "*/\n",
       "\n",
       "/* Pipeline and ColumnTransformer style (default) */\n",
       "\n",
       "#sk-container-id-2 div.sk-toggleable {\n",
       "  /* Default theme specific background. It is overwritten whether we have a\n",
       "  specific estimator or a Pipeline/ColumnTransformer */\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "/* Toggleable label */\n",
       "#sk-container-id-2 label.sk-toggleable__label {\n",
       "  cursor: pointer;\n",
       "  display: flex;\n",
       "  width: 100%;\n",
       "  margin-bottom: 0;\n",
       "  padding: 0.5em;\n",
       "  box-sizing: border-box;\n",
       "  text-align: center;\n",
       "  align-items: start;\n",
       "  justify-content: space-between;\n",
       "  gap: 0.5em;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 label.sk-toggleable__label .caption {\n",
       "  font-size: 0.6rem;\n",
       "  font-weight: lighter;\n",
       "  color: var(--sklearn-color-text-muted);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 label.sk-toggleable__label-arrow:before {\n",
       "  /* Arrow on the left of the label */\n",
       "  content: \"▸\";\n",
       "  float: left;\n",
       "  margin-right: 0.25em;\n",
       "  color: var(--sklearn-color-icon);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 label.sk-toggleable__label-arrow:hover:before {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "/* Toggleable content - dropdown */\n",
       "\n",
       "#sk-container-id-2 div.sk-toggleable__content {\n",
       "  max-height: 0;\n",
       "  max-width: 0;\n",
       "  overflow: hidden;\n",
       "  text-align: left;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-toggleable__content.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-toggleable__content pre {\n",
       "  margin: 0.2em;\n",
       "  border-radius: 0.25em;\n",
       "  color: var(--sklearn-color-text);\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-toggleable__content.fitted pre {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n",
       "  /* Expand drop-down */\n",
       "  max-height: 200px;\n",
       "  max-width: 100%;\n",
       "  overflow: auto;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {\n",
       "  content: \"▾\";\n",
       "}\n",
       "\n",
       "/* Pipeline/ColumnTransformer-specific style */\n",
       "\n",
       "#sk-container-id-2 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator-specific style */\n",
       "\n",
       "/* Colorize estimator box */\n",
       "#sk-container-id-2 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-label label.sk-toggleable__label,\n",
       "#sk-container-id-2 div.sk-label label {\n",
       "  /* The background is the default theme color */\n",
       "  color: var(--sklearn-color-text-on-default-background);\n",
       "}\n",
       "\n",
       "/* On hover, darken the color of the background */\n",
       "#sk-container-id-2 div.sk-label:hover label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "/* Label box, darken color on hover, fitted */\n",
       "#sk-container-id-2 div.sk-label.fitted:hover label.sk-toggleable__label.fitted {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator label */\n",
       "\n",
       "#sk-container-id-2 div.sk-label label {\n",
       "  font-family: monospace;\n",
       "  font-weight: bold;\n",
       "  display: inline-block;\n",
       "  line-height: 1.2em;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-label-container {\n",
       "  text-align: center;\n",
       "}\n",
       "\n",
       "/* Estimator-specific */\n",
       "#sk-container-id-2 div.sk-estimator {\n",
       "  font-family: monospace;\n",
       "  border: 1px dotted var(--sklearn-color-border-box);\n",
       "  border-radius: 0.25em;\n",
       "  box-sizing: border-box;\n",
       "  margin-bottom: 0.5em;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-estimator.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "/* on hover */\n",
       "#sk-container-id-2 div.sk-estimator:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-estimator.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Specification for estimator info (e.g. \"i\" and \"?\") */\n",
       "\n",
       "/* Common style for \"i\" and \"?\" */\n",
       "\n",
       ".sk-estimator-doc-link,\n",
       "a:link.sk-estimator-doc-link,\n",
       "a:visited.sk-estimator-doc-link {\n",
       "  float: right;\n",
       "  font-size: smaller;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  border-radius: 1em;\n",
       "  height: 1em;\n",
       "  width: 1em;\n",
       "  text-decoration: none !important;\n",
       "  margin-left: 0.5em;\n",
       "  text-align: center;\n",
       "  /* unfitted */\n",
       "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-unfitted-level-1);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted,\n",
       "a:link.sk-estimator-doc-link.fitted,\n",
       "a:visited.sk-estimator-doc-link.fitted {\n",
       "  /* fitted */\n",
       "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-1);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "div.sk-estimator:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "div.sk-estimator.fitted:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "/* Span, style for the box shown on hovering the info icon */\n",
       ".sk-estimator-doc-link span {\n",
       "  display: none;\n",
       "  z-index: 9999;\n",
       "  position: relative;\n",
       "  font-weight: normal;\n",
       "  right: .2ex;\n",
       "  padding: .5ex;\n",
       "  margin: .5ex;\n",
       "  width: min-content;\n",
       "  min-width: 20ex;\n",
       "  max-width: 50ex;\n",
       "  color: var(--sklearn-color-text);\n",
       "  box-shadow: 2pt 2pt 4pt #999;\n",
       "  /* unfitted */\n",
       "  background: var(--sklearn-color-unfitted-level-0);\n",
       "  border: .5pt solid var(--sklearn-color-unfitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted span {\n",
       "  /* fitted */\n",
       "  background: var(--sklearn-color-fitted-level-0);\n",
       "  border: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link:hover span {\n",
       "  display: block;\n",
       "}\n",
       "\n",
       "/* \"?\"-specific style due to the `<a>` HTML tag */\n",
       "\n",
       "#sk-container-id-2 a.estimator_doc_link {\n",
       "  float: right;\n",
       "  font-size: 1rem;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  border-radius: 1rem;\n",
       "  height: 1rem;\n",
       "  width: 1rem;\n",
       "  text-decoration: none;\n",
       "  /* unfitted */\n",
       "  color: var(--sklearn-color-unfitted-level-1);\n",
       "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 a.estimator_doc_link.fitted {\n",
       "  /* fitted */\n",
       "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-1);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "#sk-container-id-2 a.estimator_doc_link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 a.estimator_doc_link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "</style><div id=\"sk-container-id-2\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>LGBMRegressor(learning_rate=0.3, n_estimators=50, verbosity=-1)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-2\" type=\"checkbox\" checked><label for=\"sk-estimator-id-2\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow\"><div><div>LGBMRegressor</div></div><div><span class=\"sk-estimator-doc-link fitted\">i<span>Fitted</span></span></div></label><div class=\"sk-toggleable__content fitted\"><pre>LGBMRegressor(learning_rate=0.3, n_estimators=50, verbosity=-1)</pre></div> </div></div></div></div>"
      ],
      "text/plain": [
       "LGBMRegressor(learning_rate=0.3, n_estimators=50, verbosity=-1)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = LGBMRegressor(num_leaves=31, n_estimators=50,learning_rate=0.3, verbosity=-1)\n",
    "model.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c1b8f543-a633-47f8-981a-c701dd223ab1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9558624845475153"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_pred = model.predict(X_test)\n",
    "\n",
    "r2 = r2_score(y_test, model.predict(X_test))\n",
    "r2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd69d925-dbb6-4124-b640-a050bb582ffe",
   "metadata": {},
   "source": [
    "效果差不多"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f44cdd9f-52ec-49a6-982b-970b65b7832f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
